﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
#if WINDOWS_PHONE || IOS
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Input;
#endif

namespace Brain
{
#if WINDOWS_PHONE || IOS
    public class WP7DefaultInputManager : IInputManager
    {
        #region Not Used
        public bool IsKeyDown(Enum Key)
        {
            return false;
        }

        public bool IsKeyUp(Enum Key)
        {
            return false;
        }

        public bool IsKeyPress(Enum Key)
        {
            return false;
        }

        public void SetMousePosition(int X, int Y)
        {            
        }

        public Vector2 GetMousePosition()
        {
            return Vector2.Zero;
        }

        public Vector2 GetMouseDifference()
        {
            return Vector2.Zero;
        }

        public bool IsMouseLeftDown()
        {
            return false;
        }        

        public bool IsMouseRightDown()
        {
            return false;
        }

        public bool IsMouseMiddleDown()
        {
            return false;
        }

        public bool IsMouseLeftUp()
        {
            return false;
        }

        public bool IsMouseRightUp()
        {
            return false;
        }

        public bool IsMouseMiddleUp()
        {
            return false;
        }

        public bool IsMouseLeftClick()
        {
            return false;
        }

        public bool IsMouseRightClick()
        {
            return false;
        }

        public bool IsMouseMiddleClick()
        {
            return false;
        }


#endregion

        public List<GestureSample> Gestures { get; set; }

        public TouchCollection Touches { get; set; }
        public TouchCollection LastTouches { get; set; }

        public GamePadState LastGamePadState;
        public GamePadState GamePadState;
        public WP7DefaultInputManager()
        {
            Gestures = new List<GestureSample>();

            Touches = TouchPanel.GetState();
            LastTouches = Touches;

            ShouldUpdate = true;

            GamePadState = GamePad.GetState(PlayerIndex.One);
            LastGamePadState = GamePadState;
        }

        public void Reset()
        {
            Gestures = new List<GestureSample>();
        }

        public bool ShouldUpdate { get; set; }

        public void Update()
        {
            LastGamePadState = GamePadState;
            GamePadState = GamePad.GetState(PlayerIndex.One);

            LastTouches = Touches;
            Touches = TouchPanel.GetState();

            Gestures.Clear();
            if (TouchPanel.EnabledGestures != GestureType.None)
            {
                while (TouchPanel.IsGestureAvailable)
                {
                    GestureSample sample = TouchPanel.ReadGesture();
                    Gestures.Add(sample);
                }
            }
        }

        public bool IsButtonDown(Enum Button, PlayerIndex PlayerIndex)
        {
            return IsAnyPlayerButtonDown(Button);
        }

        public bool IsButtonUp(Enum Button, PlayerIndex PlayerIndex)
        {
            return IsAnyPlayerButtonUp(Button);
        }

        public bool IsButtonPress(Enum Button, PlayerIndex PlayerIndex)
        {
            return IsAnyPlayerButtonPress(Button);
        }

        public bool IsAnyPlayerButtonDown(Enum Button)
        {
            return GamePadState.IsButtonDown((Buttons)Button);
        }

        public bool IsAnyPlayerButtonUp(Enum Button)
        {
            return GamePadState.IsButtonUp((Buttons)Button);
        }

        public bool IsAnyPlayerButtonPress(Enum Button)
        {
            return GamePadState.IsButtonDown((Buttons)Button) &&
                 LastGamePadState.IsButtonUp((Buttons)Button);
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }


        public Vector2 GetLeftThumbstick(PlayerIndex PlayerIndex)
        {
            return Vector2.Zero;
        }

        public Vector2 GetRightThumbstick(PlayerIndex PlayerIndex)
        {
            return Vector2.Zero;
        }

        public float GetLeftTrigger(PlayerIndex PlayerIndex)
        {
            return 0;
        }

        public float GetRightTrigger(PlayerIndex PlayerIndex)
        {
            return 0;
        }


        public int GetMouseWheelValue()
        {
            return 0;
        }

        public int GetMouseWheelDifference()
        {
            return 0;
        }
    }
#endif
}
